package com.ruoyi.patrol.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.patrol.domain.PtLineStaff;
import com.ruoyi.patrol.domain.PtLineTask;
import com.ruoyi.patrol.domain.vo.ScheduleInfo;
import com.ruoyi.patrol.mapper.PtLineStaffMapper;
import com.ruoyi.patrol.mapper.PtSiteRecordStaffMapper;
import com.ruoyi.patrol.service.IPtLineStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 巡查排班(行政)Service业务层处理
 *
 * @author mlb
 * @date 2022-11-21
 */
@Service
public class PtLineStaffServiceImpl implements IPtLineStaffService {
    @Autowired
    private PtLineStaffMapper PtLineStaffMapper;
    @Autowired
    private PtSiteRecordStaffMapper ptSiteRecordStaffMapper;

    /**
     * 查询巡查排班(行政)
     *
     * @param id 巡查排班(行政)ID
     * @return 巡查排班(行政)
     */
    @Override
    public PtLineStaff selectPtLineStaffById(Long id) {
        PtLineStaff ptLineStaff = PtLineStaffMapper.selectPtLineStaffById(id);
        PtLineTask ptLineTask = new PtLineTask();
        ptLineTask.setLineId(ptLineStaff.getLineId());
        ptLineTask.setGroupId(ptLineStaff.getGroupId());
        return ptLineStaff;
    }

    /**
     * 查询巡查排班(行政)列表
     *
     * @param PtLineStaff 巡查排班(行政)
     * @return 巡查排班(行政)
     */
    @Override
    @DataScope(deptAlias = "l")
    public List<PtLineStaff> selectPtLineStaffList(PtLineStaff PtLineStaff) {
        return PtLineStaffMapper.selectPtLineStaffList(PtLineStaff);
    }

    @Override
    public List<ScheduleInfo> listUndone(ScheduleInfo scheduleInfo) {
        return PtLineStaffMapper.listUndone(scheduleInfo);
    }

    /**
     * 新增巡查排班(行政)
     *
     * @param ptLineStaff 巡查排班(行政)
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPtLineStaff(PtLineStaff ptLineStaff) {
        int result = 0;
        for (String date : ptLineStaff.getDates()) {
            ptLineStaff.setDate(DateUtils.parseDate(date));
            ptLineStaff.setCreateTime(DateUtils.getNowDate());
            result = PtLineStaffMapper.insertPtLineStaff(ptLineStaff);
            ptSiteRecordStaffMapper.insertPtSiteRecordStaffs(ptLineStaff.getId());
        }

        return result;
    }

    /**
     * 修改巡查排班(行政)
     *
     * @param PtLineStaff 巡查排班(行政)
     * @return 结果
     */
    @Override
    public int updatePtLineStaff(PtLineStaff PtLineStaff) {
        PtLineStaff.setUpdateTime(DateUtils.getNowDate());
        return PtLineStaffMapper.updatePtLineStaff(PtLineStaff);
    }

    /**
     * 批量删除巡查排班(行政)
     *
     * @param ids 需要删除的巡查排班(行政)ID
     * @return 结果
     */
    @Override
    public int deletePtLineStaffByIds(Long[] ids) {
        return PtLineStaffMapper.deletePtLineStaffByIds(ids);
    }

    /**
     * 删除巡查排班(行政)信息
     *
     * @param id 巡查排班(行政)ID
     * @return 结果
     */
    @Override
    public int deletePtLineStaffById(Long id) {
        return PtLineStaffMapper.deletePtLineStaffById(id);
    }

    @Override
    public long selectCount(PtLineStaff ptLineStaff) {
        return PtLineStaffMapper.selectCount(ptLineStaff);
    }
}
